;
(function(win, $) {
    var extend = function(object1, object2) {
        var _F = function() {
            this._super && $.isFunction(this._super.initial) && this._super.initial.apply(this, arguments);
            $.isFunction(this.initial) && this.initial.apply(this, arguments);
        }
        if ($.isPlainObject(object1)) {
            for (var i in object1) {
                _F.prototype[i] = object1[i];
            }
        }
        if ($.isPlainObject(object2)) {
            for (var i in object2) {
                _F.prototype[i] = object2[i];
            }
            _F.prototype._super = object1 || {};
        }
        return _F;
    }
    var isElement = function(element) {
        if (typeof element == 'object' && element[0] && element[0].nodeName) {
            return true;
        } else {
            return false;
        }
    }
    var select = {
        options: {
            style: 'think-select',
            valueField: 'value',
            textField: 'text',
            placeholder: '请选择',
            width: null,
            height: 240,
            appendTo: 'body',
            selected: null,
            disabled: false,
            multiple: false,
            onSelect: null,
            onChange: null,
            onFocus: null,
            onBlur: null,
            onLoad: null,
            onCreate: null
        },
        selectEle: '<div/>',
        listEle: '<div class="think-select-list"></div>',
        placeholderEle: '<div class="think-select-placeholder"></div>',
        _selectedList: [],
        _selected: function(index) {
            var _selected = $.type(index) == 'object' ? index : this.itemEle.eq(index);
            if (this._isMultiple) {
                var _inSelectedIndex = $.inArray(_selected[0], this._selectedList);
                if (_inSelectedIndex >= 0) {
                    this._selectedList.splice(_inSelectedIndex, 1);
                    _selected.removeClass('think-select-selected');
                } else {
                    this._selectedList.push(_selected[0]);
                    _selected.addClass('think-select-selected');
                }
            } else {
                this.itemEle.removeClass('think-select-selected');
                this._selectedList = [_selected];
                _selected.addClass('think-select-selected');
            }
        },
        selected: function(index) {
            if (arguments.length) {
                var _selected = this.itemEle.eq(index);
                if (typeof index != 'number' && !index) {
                    this._selectedList = [];
                    this.itemEle.removeClass('think-select-selected');
                } else {
                    this._selected.call(this, _selected);
                }
                $.isFunction(this.options.onSelect) && this.options.onSelect.call(this);
            } else {
                return this._selectedList;
            }
            return this;
        },
        multiple: function(status) {
            if (arguments.length) {
                this._isMultiple = status ? true : false;
            } else {
                return this._isMultiple;
            }
            return this;
        },
        add: function(data) {
            var _html = '',
                _length = data.length;
            for (var i = 0; i < _length; i++) {
                _html += '<div class="think-select-item" data-value="' + (data[i][this.options.valueField] || '') + '"><i class="think-select-check">&nbsp;</i><span class="think-select-text">' + data[i][this.options.textField] + '</span></div>';
            }
            this.listEle.append(_html);
            this.itemEle = this.listEle.find('.think-select-item');
            return this;
        },
        remove: function(index) {
            this.itemEle.eq(index).remove();
            this.itemEle = this.listEle.find('.think-select-item');
            return this;
        },
        empty: function() {
            this.itemEle = this.listEle.find('.think-select-item').remove();
            this.selected(false);
            return this;
        },
        disabled: function(status) {
            if (arguments.length) {
                this._isDisabled = status ? true : false;
                if (this._isDisabled) {
                    this.selectEle.addClass('think-select-disabled');
                } else {
                    this.selectEle.removeClass('think-select-disabled');
                }
            } else {
                return this._isDisabled;
            }
        },
        getValue: function() {
            var _value = [];
            $.each(this._selectedList, function() {
                _value.push($(this).data('value'));
            });
            return _value;
        },
        width: function(width) {
            this.listEle.css('width', width);
            return this;
        },
        height: function(height) {
            this.listEle.css('height', height);
            return this;
        }
    }
    var selectBox = extend(select, {
        initial: function(element, data, options) {
            var _self = this;
            $.isPlainObject(data) && (options = data) && (data = []);
            this.options = $.extend({}, this._super.options, options);
            this.selectEle = $(this._super.selectEle);
            this.listEle = $(this._super.listEle);
            this.placeholderEle = $(this._super.placeholderEle);
            this.selectEle.appendTo($(this.options.appendTo));
            this.selectEle.addClass(this.options.style);
            this.selectEle.css('position', 'absolute');
            this.listEle.appendTo(this.selectEle);
            if (typeof this.options.placeholder == 'string') {
                this.placeholderEle.html(this.options.placeholder);
                this.placeholderEle.prependTo(this.listEle);
                this.placeholderEle.click(function(event) {
                    _self.selected(false);
                    _self.close();
                    event.stopPropagation();
                });
            }
            this.add(data);
            this.multiple(this.options.multiple);
            this.listEle.delegate('.think-select-item', 'click', function() {
                _self.selected(_self.itemEle.index($(this)));
                if (!_self._isMultiple) {
                    _self.close();
                }
                return false;
            });
            this.handlerEle = element;
            this.handlerEle.addClass('think-select-enter');
            this.comoboxEle = $('<span class="think-select"></span>');
            this.handlerEle.after(this.comoboxEle);
            this.handlerEle.appendTo(this.comoboxEle);
            this.switchEle = $('<span class="think-select-switch">&nbsp;</span>');
            this.switchEle.appendTo(this.comoboxEle);
            this.handlerEle.click(function() {
                _self.open();
                return false;
            });
            $('body').click(function() {
                _self.close();
            });
            this.switchEle.click(function(event) {
                if (_self._isOpen) {
                    _self.close();
                } else {
                    _self.open();
                }
                event.stopPropagation();
            });
            this._selected(this.options.selected);
            this.disabled(this.options.disabled);
            this.width(this.options.width);
            this.height(this.options.height);
            this.options.open ? this.open() : this.close();
            $.isFunction(this.options.onCreate) && this.options.onCreate.call(this);
        },
        open: function() {
            if (!this._isDisabled) {
                this.selectEle.addClass('think-select-open').removeClass('think-select-close');
                this.comoboxEle.addClass('think-select-open').removeClass('think-select-close');
                this.selectEle.css({
                    'left': this.handlerEle.offset().left,
                    'top': this.handlerEle.offset().top + this.handlerEle.outerHeight()
                })
                this._isOpen = true;
            }
        },
        close: function() {
            this.selectEle.addClass('think-select-close').removeClass('think-select-open');
            this.comoboxEle.addClass('think-select-close').removeClass('think-select-open');
            this._isOpen = false;
        },
        selected: function(index) {
            if (!this._isDisabled) {
                var _text = [],
                    _return;
                _return = this._super.selected.apply(this, arguments);
                $.each(this._selectedList, function() {
                    _text.push($(this).find('.think-select-text').text());
                });
                this.handlerEle.val(_text.join('+'));
                if (this._selectedList.length) {
                    this.comoboxEle.removeClass('think-select-empty');
                } else {
                    this.comoboxEle.addClass('think-select-empty');
                    this.handlerEle.val(this.options.placeholder);
                }
                return _return;
            }
        },
        width: function(width) {
            if (arguments.length) {
                if (width) {
                    this.selectEle.width(width);
                } else {
                    this.selectEle.css('min-width', this.handlerEle.outerWidth());
                }
            } else {
                return this.listEle.width();
            }
        },
        height: function(height) {
            if (arguments.length) {
                if (height) {
                    this.listEle.css('max-height', height);
                }
            } else {
                return this.listEle.height();
            }
        },
        disabled: function(status) {
            var _return = this._super.disabled.apply(this, arguments);
            if (this._isDisabled) {
                this.close();
                this.handlerEle.attr('disabled', true);
                this.comoboxEle.addClass('think-select-disabled');
            } else {
                this.handlerEle.attr('disabled', false);
                this.comoboxEle.removeClass('think-select-disabled');
            }
            return _return;
        }
    });
    var selectList = extend(select, {
        initial: function(data, options) {
            var _self = this;
            $.isPlainObject(data) && (options = data) && (data = []);
            this.options = $.extend({}, this._super.options, options);
            this.selectEle = $(this._super.selectEle);
            this.listEle = $(this._super.listEle);
            this.placeholderEle = $(this._super.placeholderEle);
            this.selectEle.appendTo($(this.options.appendTo));
            this.selectEle.addClass(this.options.style);
            this.selectEle.css('position', 'absolute');
            this.listEle.appendTo(this.selectEle);
            if (typeof this.options.placeholder == 'string') {
                this.placeholderEle.html(this.options.placeholder);
                this.placeholderEle.prependTo(this.listEle);
                this.placeholderEle.click(function(event) {
                    _self.selected(false);
                    event.stopPropagation();
                });
            }
            this.add(data);
            this.multiple(this.options.multiple);
            this.listEle.delegate('.think-select-item', 'click', function() {
                _self.selected(_self.itemEle.index($(this)));
            });
            this._selected(this.options.selected);
            this.disabled(this.options.disabled);
            this.width(this.options.width);
            this.height(this.options.height);
            $.isFunction(this.options.onCreate) && this.options.onCreate.call(this);
        }
    });
    $.extend($.fn, {
        selectList: function(data, options) {
            return new selectList(data, $.extend(options, {
                appendTo: $(this)
            }));
        },
        selectBox: function(data, options) {
            return new selectBox($(this), data, options);
        }
    })
})(window, jQuery);
